#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> findSubstring(string s, vector<string> &words)
    {
        map<string, int> mp1, mp2, mp;
        int count = 0;
        vector<int> ans;
        int n = s.size(), m = words[0].size();
        for (auto &ch : words)
        {
            mp[ch]++;
        }
        for (int i = 0; i < m; i++)
        {
            mp1.clear();
            count = 0;
            for (int l = i, r = i; r + m <= n; r += m)
            {
                string in = s.substr(r, m);
                mp1[in]++;
                if (mp1[in] <= mp[in])
                {
                    count++;
                }
                if (r - l + 1 > m * words.size())
                {
                    string out = s.substr(l, m);
                    if (mp1[out] <= mp[out])
                        count--;
                    mp1[out]--;
                    l += m;
                }
                if (count == words.size())
                {
                    ans.push_back(l);
                }
            }
        }
        return ans;
    }
};